package cn.org.xwb.main.controller;

import cn.org.xwb.main.annotation.mail.MailLegalAnno;
import cn.org.xwb.main.annotation.mail.MailLegalParamAnno;
import cn.org.xwb.main.annotation.string.StrNotNullAnno;
import cn.org.xwb.main.annotation.string.StrNotNullParamAnno;
import cn.org.xwb.main.dto.BaseRespResult;
import cn.org.xwb.main.service.LoginService;
import cn.org.xwb.main.vo.LoginInfoByMailVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Log4j2
@RestController
@RequestMapping("/login")
public class LoginController {

    /**
     * 登录业务层对象
     */
    @Autowired
    private LoginService loginService;

    @PostMapping("/mail/code")
    @StrNotNullAnno
    @MailLegalAnno
    public BaseRespResult<Object> sendMailCode(
            @RequestParam("mail")
            @StrNotNullParamAnno
            @MailLegalParamAnno
            String mail
    ) {
        log.info("将要向 {} 发送邮箱验证码", mail);
        return loginService.sendMailCode(mail);
    }

    @PostMapping("/mail")
    public BaseRespResult<Object> loginByMail(@RequestBody LoginInfoByMailVo loginInfoByMailVo) {
        log.info("用户 {} 正在使用邮箱进行登录，输入的验证码：{}", loginInfoByMailVo.getMail(), loginInfoByMailVo.getCode());
        return loginService.loginByMail(loginInfoByMailVo);
    }

}
